#include "manager.h"

Manager::Manager(QObject *parent) : QObject(parent)
{

    this->model=new BaseModel();
    this->w=new OpenView();
    if(selectDatabase())
    connect(this->w,&OpenView::isHide,this,&Manager::onAnimationOpenTomain);
    else
    connect(this->w,&OpenView::isHide,this,&Manager::onAnimationOpenToset);

}

Manager::~Manager()
{
    delete main;
    delete w;
}

void Manager::start()
{
    w->show();
}

void Manager::onAnimationOpenToset()
{
    this->w->hide();
    this->setWin=new SetWidget();
    connect(this->setWin,&SetWidget::ensureFinished,this,&Manager::setEnsureFinished);
    this->setWin->show();

}

void Manager::setEnsureFinished()
{

    if(this->main!=nullptr)
    {
         this->setWin->hide();
        this->main->getDecode()->setIsStop(false);
        this->main->getEncode()->setIsStop(false);
         this->main->show();

    }
    else {
       this->main=new MainWidget();
        connect(this->main,&MainWidget::TosetWidget,this,&Manager::TosetWidget);
        connect(this->main,&MainWidget::TologinWidget,this,&Manager::TologinWidget);
        connect(this->main,&MainWidget::ToScenepicture,this,&Manager::MainToPicture);
        connect(this->main,&MainWidget::ToVideoSelect,this,&Manager::MainToVideoSelect);
        connect(this->main,&MainWidget::ToTransCode,this,&Manager::MainToTransCode);
        connect(this->main,&MainWidget::ToLog,this,&Manager::MainToLog);
       this->setWin->hide();
       this->main->show();
    }
}

void Manager::onAnimationOpenTomain()
{
    this->w->hide();
    this->main=new MainWidget();
//    this->setWin=new SetWidget();
    connect(this->main,&MainWidget::TosetWidget,this,&Manager::TosetWidget);
    connect(this->main,&MainWidget::TologinWidget,this,&Manager::TologinWidget);
    connect(this->main,&MainWidget::ToScenepicture,this,&Manager::MainToPicture);
    connect(this->main,&MainWidget::ToVideoSelect,this,&Manager::MainToVideoSelect);
    connect(this->main,&MainWidget::ToTransCode,this,&Manager::MainToTransCode);
    connect(this->main,&MainWidget::ToLog,this,&Manager::MainToLog);
    this->main->show();
}

void Manager::TosetWidget()
{
    if(this->setWin==nullptr)
    {
        this->setWin=new SetWidget();
        connect(this->setWin,&SetWidget::ensureFinished,this,&Manager::setEnsureFinished);
        this->main->hide();
        this->setWin->show();
    }
    else {
        this->main->hide();
        this->setWin->show();
    }
}

void Manager::receptionLogin()
{
    this->setWin->hide();
    this->main->show();
}

void Manager::TologinWidget(int sign)
{

    if(this->loginWin)
    {
        this->main->hide();
\
        this->loginWin->show();
    }
    else
    {
        this->loginWin=new LoginWidget();
        connect(this->loginWin,&LoginWidget::LoginToRet,this,&Manager::LoginToWhere);
        connect(this->loginWin,&LoginWidget::RetToMain,this,&Manager::LoginToMain);
        this->loginWin->setIndex(sign);
        this->main->hide();
        this->loginWin->show();
    }


}




void Manager::LoginToWhere(int index)
{
    //把相关信息写入到日志表

    this->main->setIsLogin(true);
    this->main->getLogIn_button()->setText("退出");
    qDebug()<<"index="<<index;



    if(index==0)
    {
        //现场拍照界面
        //拍照没有界面所以回到主界面
        this->loginWin->hide();

        this->main->show();
        this->main->getDecode()->setIsStop(false);
        this->main->getEncode()->setIsStop(false);
    }
    else if(index==1)
    {
        //录像回放界面

        if(this->videoWin==nullptr)
        {
            this->videoWin=new VideoSelectWidget();
            connect(this->videoWin,&VideoSelectWidget::SelectToMain,this,&Manager::VideoSelectToMain);
            this->loginWin->hide();
            this->videoWin->show();
        }
        else {
            this->loginWin->hide();
            this->videoWin->show();
        }

    }
    else if(index==2)
    {
        //现场照片界面
        if(this->photoWin==nullptr)
        {
            this->photoWin=new PhotoViewWidget();
            connect(this->photoWin,&PhotoViewWidget::PhotoToMain,this,&Manager::PictureToMain);
            this->loginWin->hide();
            this->photoWin->show();
        }
        else {
            this->loginWin->hide();
            this->photoWin->show();
        }
    }
    else if(index==3)
    {
        //日志查看界面
        if(logWin==nullptr)
        {
            this->logWin=new LogWidget();
            connect(this->logWin,&LogWidget::RetToMain,this,&Manager::LogToMain);
            this->loginWin->hide();
            this->logWin->show();
        }
        else
        {
            this->loginWin->hide();
            this->logWin->show();
        }
    }
    else if(index==4)
    {
        //转码界面

        if(this->transcodWin==nullptr)
        {
            this->transcodWin=new TranscodeWidget();
            connect(this->transcodWin,&TranscodeWidget::ToMain,this,&Manager::TransCodeToMain);

            this->loginWin->hide();
            this->transcodWin->show();
        }
        else {
            this->loginWin->hide();

            this->transcodWin->show();
        }
    }
    else if(index==5)
    {
        //设置界面，可能没有创建设置界面
        if(setWin==nullptr)
        {
            this->setWin=new SetWidget();
            connect(this->setWin,&SetWidget::ensureFinished,this,&Manager::setEnsureFinished);
            connect(this->main,&MainWidget::ToScenepicture,this,&Manager::MainToPicture);
            this->loginWin->hide();
            this->setWin->show();
        }
        else
        {
            this->loginWin->hide();
            this->setWin->show();
        }
    }
    else if(index==6)
    {
        //返回主界面//因为是登陆所以登陆完成返回主界面
        this->loginWin->hide();
        if(main)
        if(this->main->getDecode())
        this->main->getDecode()->setIsStop(false);
        this->main->getEncode()->setIsStop(false);
        this->main->show();
    }

}

void Manager::LoginToMain()
{
    this->loginWin->hide();
    this->main->getDecode()->setIsStop(false);
    this->main->getEncode()->setIsStop(false);
    this->main->show();
}

void Manager::MainToPicture()
{
    if(this->photoWin==nullptr)
    {
        this->photoWin=new PhotoViewWidget();
        connect(this->photoWin,&PhotoViewWidget::PhotoToMain,this,&Manager::PictureToMain);

        this->main->hide();
        this->photoWin->show();
    }
    else {
        this->main->hide();
        this->photoWin->show();
    }
}

void Manager::PictureToMain()
{
    this->photoWin->hide();
    this->main->getDecode()->setIsStop(false);
    this->main->getEncode()->setIsStop(false);

    this->main->show();
}


void Manager::MainToVideoSelect()
{
    if(this->videoWin==nullptr)
    {
        this->videoWin=new VideoSelectWidget();
        connect(this->videoWin,&VideoSelectWidget::SelectToMain,this,&Manager::VideoSelectToMain);
        this->main->hide();
        this->videoWin->show();
    }
    else {
        this->main->hide();
        this->videoWin->show();

    }
}

void Manager::VideoSelectToMain()
{
    qDebug()<<"进来了";
    this->videoWin->hide();
    this->main->getDecode()->setIsStop(false);
    this->main->getEncode()->setIsStop(false);
    this->main->show();
}

void Manager::MainToTransCode()
{
    if(this->transcodWin==nullptr)
    {
        this->transcodWin=new TranscodeWidget();
        connect(this->transcodWin,&TranscodeWidget::ToMain,this,&Manager::TransCodeToMain);
        this->main->hide();
        this->transcodWin->show();
    }
    else {
        this->main->hide();
        this->transcodWin->SelectDataBase();
        this->transcodWin->show();

    }
}

void Manager::TransCodeToMain()
{
    this->transcodWin->hide();
    this->main->getDecode()->setIsStop(false);
    this->main->getEncode()->setIsStop(false);
    this->main->show();
}

void Manager::LogToMain()
{
    this->logWin->hide();
    this->main->getDecode()->setIsStop(false);
    this->main->getEncode()->setIsStop(false);
    this->main->show();
}

void Manager::MainToLog()
{
    if(logWin==nullptr)
    {
        this->logWin=new LogWidget();
        connect(this->logWin,&LogWidget::RetToMain,this,&Manager::LogToMain);
        this->main->hide();
        this->logWin->show();
    }
    else
    {
        this->main->hide();
        this->logWin->show();
    }
}

bool Manager::selectDatabase()
{
    QString sql("select * from device_info;");
    bool st=this->model->selectIsEmpty((char *)sql.toStdString().c_str());
    return st;
}
